home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Sound / SndPlayDoubleBuffer / _source / LDandFix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-15  |  2.8 KB  |  88 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Routines demonstrating how to convert to and from long double and Fixed types.
  5. **
  6. **    by Mark Cookson, Apple Developer Technical Support
  7. **
  8. **    File:    LDandFix.c
  9. **
  10. **    Copyright ©1996 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "Apple Sample
  17. **    Code" after having made changes. If you're going to re-distribute the
  18. **    source, we require that you make it clear in the source that the code
  19. **    was descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #include "LDandFix.h"
  23.  
  24. Fixed            ASoundLongDoubleToFix    (long double theLD)
  25. {
  26. /*
  27.     A Fixed number is of the type 12345.67890.  It is 32 bits in size with the
  28.     high order bits representing the significant value (that before the point)
  29.     and the lower 16 bits representing the fractional part of the number.
  30.  
  31.     The Sound Manager further complicates matters by using Fixed numbers, but
  32.     needing to represent numbers larger than what the Fixed is capable of.
  33.  
  34.     To do this the Sound Manager treats the sign bit as having the value 32768
  35.     which will cause any number greater or equal to 32768 to look like it is
  36.     negative.
  37.  
  38.     This routine is designed to "do the right thing" and convert any long double
  39.     into the Fixed number it represents.
  40.  
  41.     long double is the input type because AIFF files use extended80 numbers and
  42.     there are routines that will convert from an extended80 to a long double.
  43.  
  44.     A long double has far greater precision than a Fixed, so any number whose
  45.     significant or fraction is larger than 65535 will not convert correctly.
  46. */
  47.  
  48.     unsigned long    theResult        = 0;
  49.     unsigned short    theSignificant    = 0,
  50.                     theFraction        = 0;
  51.  
  52.     if (theLD < kMaxValue) {
  53.         theSignificant = theLD;
  54.         theFraction = theLD - theSignificant;
  55.         if (theFraction > kMaxValue) {
  56.             /* Won't be able to convert */
  57.             theSignificant    = 0;
  58.             theFraction        = 0;
  59.         }
  60.     }
  61.  
  62.     theResult |= theSignificant;
  63.     theResult = theResult << (sizeof (unsigned short) * kBitsPerByte);
  64.     theResult |= theFraction;
  65.  
  66.     return theResult;
  67. }
  68.  
  69. /*
  70.     I'm still trying to figure out why I need the correction factor, but the
  71.     number returned by this function is only for our own internal calculation
  72.     of buffer sizing so we can afford to loose a little precision in the
  73.     fraction.
  74. */
  75. long double        ASoundFixToLongDouble    (Fixed theFixed)
  76. {
  77.     long double        theResult    = 0.0;
  78.     unsigned short    high16Bits    = 0,
  79.                     low16Bits    = 0;
  80.  
  81.     high16Bits = theFixed >> (sizeof (unsigned short) * kBitsPerByte);
  82.     low16Bits = theFixed + 0x496E;        /* Correction factor */
  83.  
  84.     theResult = high16Bits + (low16Bits * kFraction);
  85.  
  86.     return theResult;
  87. }
  88.